const adminModel = require("../model/adminModel");

// 查找用户是否存
module.exports.find = async function (username) {
  let result = await adminModel.findOne({ username: username });
  if (result) {
    return false;
  }
  if (result == null) {
    return true;
  }
};

// 注册管理员
module.exports.regAdmin = async function (obj) {
  return await adminModel.create(obj);
};

// 登录
module.exports.login = async function (obj) {
  let result = await adminModel.findOne(obj);
  if (result) {
    return result;
  }
  if (result == null) {
    return {};
  }
};
// 获取管理员数据
module.exports.adminList = async function (page) {
  let { current, size } = page;
  let currentPage = ~~current;
  let total = await adminModel.countDocuments();
  let totalPage = Math.ceil(total / size);
  let rows = await adminModel
    .find()
    .populate({ path: "goodsId" })
    .populate({ path: "userId" })
    .limit(parseInt(size))
    .skip((currentPage - 1) * size);
  return { currentPage, size, total, totalPage, rows };
};


// 条件获取管理员数据
module.exports.adminCondition = async function (searchObj, page) {
  let { current, size } = page;
  let currentPage = ~~current;
  let total = await adminModel.countDocuments();
  let totalPage = Math.ceil(total / size);
  let rows = await adminModel
    .find(searchObj)
    .populate({ path: "goodsId" })
    .populate({ path: "userId" })
    .limit(parseInt(size))
    .skip((currentPage - 1) * size);
  return { currentPage, size, total, totalPage, rows };
};


// 删除管理员数据
module.exports.deleteAdmin = async function ({ id }) {
  return await adminModel.deleteOne({ _id: id });
};


//修改匹配
module.exports.update = async function (id, content) {
  return await adminModel.updateOne({ _id: id }, content);
}






